package zzj.campus_book_sharing_platform.Controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zzj.campus_book_sharing_platform.Service.PostService;
import zzj.campus_book_sharing_platform.domain.dto.PostDto;
import zzj.campus_book_sharing_platform.uitl.ResultFormat;
import zzj.campus_book_sharing_platform.uitl.ResultUtil;

import javax.servlet.http.HttpSession;

@Tag(name = "帖子")
@RestController
@RequestMapping("/post")
public class postController {

    @Autowired
    PostService postService;

    /**
     * 帖子列表  计算帖子的权重，并以权重来划分帖子的排名，以及帖子的完成状态和帖子的类型
     *
     * @return
     */
    @GetMapping("/postList")
    @Operation(summary = "帖子-帖子列表")
    public ResultFormat postList(@RequestParam Integer currentPage){
        return ResultUtil.success(postService.queryAll(currentPage));
    }

    /**
     * 查看当前帖子详情
     *
     * @param postId
     * @return
     */
    @GetMapping("/selectById")
    @Operation(summary = "帖子-详情")
    public ResultFormat selectById(String postId){
        return ResultUtil.success(postService.selectById(postId));
    }

    /**
     * 发布帖子
     *
     * @param postDto
     * @param session
     * @return
     */
    @PostMapping("/publishPost")
    @Operation(summary = "帖子-发布")
    public ResultFormat publishPost(@RequestBody PostDto postDto, HttpSession session){
        return ResultUtil.success(postService.insert(postDto,session));
    }

    /**
     * 删除帖子（只能删除自己的）（管理员能删除别人的）
     *
     * @param postId
     * @return
     */
    @GetMapping("/deleteById")
    @Operation(summary = "帖子-删除")
    public ResultFormat delete(String postId,HttpSession session){
        return ResultUtil.success(postService.deleteById(postId,session));
    }

}
